<?php
	class MetaInformation extends AppModel
	{
		var $name = "MetaInformation";
		var $pages = array();
		var $useTable = "meta_information";
		var $fieldProperties = array(
			'pageTitle' => array('label'=>"Page title",'group'=>"add",'type'=>"text"),
			'metaTags' => array("label"=>"Meta Tags",'group'=>"add","type"=>"text"),
			'metaDescription' => array("label"=>"Meta Description",'group'=>"add","type"=>"textarea"),
		);
		function initialize()
		{
                $this->setFieldProperties('pageTitle', array(
                    'after'=>__("Leave empty to use default page title")
                ));
				$this->pages = array(
				'default' => array('label'=>__("All Pages"),'url'=>array('controller'=>"*",'action'=>"*")),
				'homepage' => array('label'=>__("Home Page"),'url'=>array('controller'=>'listings','action'=>"home")),
				'listingIndex' => array('label'=>__("Listing Index"),'url'=>array('controller'=>'listings','action'=>"index")),
				'reviewIndex' => array('label'=>__("Review Index"),'url'=>array("controller"=>"reviews",'action'=>"index")),
				'registerPage' => array("label"=>__("Register"),'url'=>array("controller"=>"users",'action'=>"register")),
				'contactus' => array('label'=>__("Contact Us"),'url'=>array("controller"=>"contacts",'action'=>"add")),
				'galleryPages' => array("label"=>__("Gallery Index"),'url'=>array("controller"=>"listing_assets","action"=>"all")),
				'listingView' => array("label"=>__("Listing View (Default information for detailed listing page, used for listings where specific information hasn't been defined.)"),'url'=>array("controller"=>"listings","action"=>"view")),
				'reviewView' => array("label"=>__("Review View (Default information for detailed review page, used for reviews where where specific information hasn't been defined.)"),'url'=>array("controller"=>"reviews","action"=>"view"))
			);
			$pagePath = APP.'views'.DS.'pages'.DS;
			uses("folder");
			$Folder = new Folder($pagePath);
			$pageFiles = $Folder->find(".*\.ctp");
			foreach($pageFiles as $pageFile)
			{
				if(is_file($pagePath.$pageFile))
				{
					$pageFile = str_replace(".ctp", "", $pageFile);
					$pageName = Inflector::humanize($pageFile);
					$pageId = "page_".$pageFile;
					$this->pages[$pageId] = array("label"=>$pageName,'url'=>array('controller'=>"pages",'action'=>$pageFile),'static_page'=>1);
				}
			}
			parent::initialize();
		}
		
		function getMetaInformation($entity,$entityId,$refresh = false)
		{
			$metaInformation = $this->find("first",array("conditions"=>array('entity'=>$entity,'entity_id'=>$entityId)));
			$metaInformation = $metaInformation[$this->alias];
			return $metaInformation;
		}
		
		function getUrlMetaInformation($controller,$action)
		{
			$presentPageName = false;
			foreach($this->pages as $pageName => $pageInfo)
			{
				if(@$pageInfo['url']['controller'] == $controller && @$pageInfo['url']['action'] == $action)
				{
					$presentPageName = $pageName;
					break;
				}
			}
			return $this->getPageMetaInformation($presentPageName);
		}
		
		function getPageMetaInformation($presentPageName,$refresh = false)
		{
			$metaInformation = $this->find("first",array("conditions"=>array('page_name'=>$presentPageName)));
			$metaInformation = $metaInformation[$this->alias];
			return $metaInformation;
		}
	}
?>